package com.virus.coronavirus.service;

import com.virus.coronavirus.error.SystemCodeEnum;
import com.virus.coronavirus.error.SystemException;
import com.virus.coronavirus.mapper.ActionMapper;
import com.virus.coronavirus.model.Action;
import com.virus.coronavirus.model.vo.ActionVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @Author: Bbeginner
 * @Date: 2022/1/18 22:11
 * @Version 1.0
 **/
@Service
public class ActionServiceImpl implements ActionService{

    @Autowired
    private ActionMapper actionMapper;

    @Override
    public Action selectList(Long id) {
        return actionMapper.selectByPrimaryKey(id);
    }

    @Transactional
    @Override
    public void add(ActionVO actionVO) throws SystemException {
        Action action = new Action();
        BeanUtils.copyProperties(actionVO,action);

        actionMapper.insert(action);
    }

    @Override
    public void deleteById(Long id) throws SystemException {
        Action action = actionMapper.selectByPrimaryKey(id);
        if(action==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的数据不存在");
        }
        actionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByUserid1(Long id) throws SystemException {
        Example example = new Example(Action.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("user_id1",id);
        actionMapper.deleteByExample(example);
       //相当于：delete from action where user_id1='id'

    }

    @Override
    public void deleteAll() throws SystemException {
        Example example = new Example(Action.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.getAllCriteria();
        actionMapper.deleteByExample(example);
    }

    @Override
    public List<Action> findByUserid1(Long id) {
        Action t = new Action();
        t.setUser_id1(id);
        List<Action> actions = actionMapper.select(t);
        return actions;
    }

    @Override
    public List<Action> findByUserid2(Long id) {
        Action t = new Action();
        t.setUser_id2(id);
        List<Action> actions = actionMapper.select(t);
        return actions;
    }

    @Override
    public List<Action> findBymeet(String meet) {
        Action t = new Action();
        t.setMeet(meet);
        List<Action> actions = actionMapper.select(t);
        return actions;
    }


}
